home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / tests / tFix24.cc < prev    next >
C/C++ Source or Header  |  1994-08-12  |  3KB  |  114 lines

  1. //
  2. // testFix24.cc : test Fix24/48 classes
  3. //
  4.  
  5. #include <Fix24.h>
  6.  
  7. // This a set of inlines instead of a macro to force the side effects
  8. // of evaluating y to happen before x is printed.
  9.  
  10. inline void check(char *x, int y) { cout << x << " = " << (y) << "\n"; }
  11. inline void check(char *x, unsigned int y) { cout << x << " = " << (y) << "\n"; }
  12. inline void check(char *x, long y) { cout << x << " = " << (y) << "\n"; }
  13. inline void check(char *x, unsigned long y) { cout << x << " = " << (y) << "\n"; }
  14. inline void check(char *x, double y) { cout << x << " = " << (y) << "\n"; }
  15. inline void check(char *x, Fix24 y) { cout << x << " = " << (y) << "\n"; }
  16. inline void check(char *x, Fix48 y) { cout << x << " = " << (y) << "\n"; }
  17.  
  18. void test24() {
  19.   cout << "Fix24: identities should be displayed\n";
  20.  
  21.   Fix24 a;        check("0",a);
  22.   Fix24 b = .5;        check(".5",b);
  23.   Fix24 c = -.5;    check("-.5",c);
  24.   Fix24 d = .1;        check(".1",d);
  25.   Fix24 e = b;        check(".5",e);
  26.  
  27.   check(".5",a = b);
  28.   check(".25",a = .25);
  29.   check("536870912",mantissa(a));
  30.   mantissa(a)=536870912;
  31.   check(".25",a);
  32.   check(".25",value(a));
  33.  
  34.   check(".25",+a);
  35.   check("-.25",-a);
  36.  
  37.   check(".1 + .5",d+b);
  38.   check(".1 - .5",d-b);
  39.   check(".1 * .5",d*b);
  40.   check(".1 *  3",d*3);
  41.   check(".1 * -3",d*-3);
  42.   check(".1 / .5",d/b);
  43.   check(".1 << 1",d<<1);
  44.   check("-.5 >> 2",c>>2);
  45.  
  46.   check(".1 == .5",d == b);
  47.   check(".1 != .5",d != b);
  48.   check(".1 > .5",d > b);
  49.   check(".5 <= -.5",b <= c);
  50.  
  51.   cout << "Fix24: range errors ignored and overflows saturated\n";
  52.   set_Fix24_overflow_handler(Fix24_overflow_saturate);
  53.   set_Fix24_range_error_handler(Fix24_ignore);
  54.  
  55.   Fix24 f = 1.1;    check("1.1",f);
  56.  
  57.   Fix24 g = .7;
  58.   check(".7 + .5",g+b);
  59.   check("-.5 - .7",c-g);
  60.   check(".5 / .1",b/d);
  61. }
  62.  
  63. void test48() {
  64.   cout << "Fix48: identities should be displayed\n";
  65.  
  66.   Fix48 a;        check("0",a);
  67.   Fix48 b = .5;        check(".5",b);
  68.   Fix48 c = -.5;    check("-.5",c);
  69.   Fix48 d = .1;        check(".1",d);
  70.   Fix48 e = b;        check(".5",e);
  71.  
  72.   check(".5",a = b);
  73.   check(".25",a = .25);
  74.   twolongs t;
  75.   t = mantissa(a);
  76.   check("536870912",t.u);
  77.   check("0",t.l);
  78.   mantissa(a)=t;
  79.   check(".25",a);
  80.   check(".25",value(a));
  81.  
  82.   check(".25",+a);
  83.   check("-.25",-a);
  84.  
  85.   check(".1 + .5",d+b);
  86.   check(".1 - .5",d-b);
  87.   check(".1 *  3",d*3);
  88.   check(".1 * -3",d*-3);
  89.   check(".1 << 1",d<<1);
  90.   check("-.5 >> 2",c>>2);
  91.  
  92.   check(".1 == .5",d == b);
  93.   check(".1 != .5",d != b);
  94.   check(".1 > .5",d > b);
  95.   check(".5 <= -.5",b <= c);
  96.  
  97.   cout << "Fix48: range errors reported and overflows reported\n";
  98.   set_Fix48_overflow_handler(Fix48_warning);
  99.   set_Fix48_range_error_handler(Fix48_warning);
  100.  
  101.   Fix48 f = 1.1;    check("1.1",f);
  102.  
  103.   Fix48 g = .7;
  104.   check(".7 + .5",g+b);
  105.   check("-.5 - .7",c-g);
  106. }
  107.  
  108. int main() {
  109.   test24();
  110.   test48();
  111.   return 0;
  112. }
  113.  
  114.